package cicd_projects

import (
	"github.com/gin-gonic/gin"
	"k8s-platform/model"
	"k8s-platform/service/cicd_projects"
	"net/http"
)

var Cicdprojects cicdprojects

type cicdprojects struct{}

// 新增
func (*cicdprojects) Create_apps(c *gin.Context) {
	var inputData = model.InputData{}

	if err := c.ShouldBindJSON(&inputData); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 90400,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	if err := cicd_projects.Create_apps(inputData); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 90500,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "创建项目部署表单成功",
		"data": nil,
	})
}

// 查看列表
func (*cicdprojects) List(c *gin.Context) {
	data, err := cicd_projects.Cicdprojects.List()
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 90500,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "查询列表成功",
		"data": data,
	})
}

// 运行
func (*cicdprojects) Run_apps(c *gin.Context) {
	var inputData = model.InputData{}

	if err := c.ShouldBindJSON(&inputData); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 90400,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	if err := cicd_projects.Run_apps(inputData); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 90500,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "运行项目部署表单成功",
		"data": nil,
	})
}

func (*cicdprojects) Delete_apps(c *gin.Context) {
	var inputData = model.InputData{}

	if err := c.ShouldBindJSON(&inputData); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 90400,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	if _, err := cicd_projects.Delete_apps(inputData); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 90500,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "删除项目成功",
		"data": nil,
	})
}
